package com.lanou3g.ptp.approval.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.lanou3g.ptp.approval.mapper.ApprovalMapper;
import com.lanou3g.ptp.entity.Approval;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.entity.Teacher;
import com.lanou3g.ptp.entity.User;
import com.lanou3g.ptp.service.ApprovalService;
import com.lanou3g.ptp.sso.service.SsoService;
import com.lanou3g.ptp.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;


@Service
public class ApprovalServiceImpl implements ApprovalService {

    @Autowired
    private ApprovalMapper approvalMapper;

    @Reference
    private SsoService ssoService;

    @Reference
    private UserService userService;

    /*
    String token,用户key
    String approval,申请文本
    Date start,开始时间
    Date over,结束时间
     */
    //App学生接口
    @Override
    public Boolean studentApproval(String token, int type, String approval,String photo,Date start, Date over) {
        boolean b = false;
        //获取当前学生信息  存入数据库  approval
        Student student = (Student) ssoService.tokenUser(token);
        Date create = new Date();//创建时间
        int i = approvalMapper.approvalById(student.getId(), type,approval, start, photo,over, create);
        if (i > 0) {
            b = true;
        }
        return b;
    }

    //学生自行查询 请假状态
    @Override
    public Approval findApproval(String token) {
        //使用token 找学生
        Student student = (Student) ssoService.tokenUser(token);
        //返回学生id 查找请假信息
        return approvalMapper.ById(student.getId());
    }

    //老师接口查询所有请假信息
    @Override
    public List<Approval> approvalAll() {
        //查询所有请假得信息
        List<Approval> approvalList = approvalMapper.findAll();
        for (Approval approval : approvalList) {
            //通过学生id查询对应用户  获取名字
            User user = userService.userById(approval.getStudentId());
            approval.setUserName(user.getName());
        }
        return approvalList;
    }

    //0(已提交)
    //	1(同意)
    //	2(拒绝)
    //老师审核接口
    //i 0 同意    1拒绝
    @Override
    public Boolean teacherApproval(String token, Long id, Long i) {
        boolean b = false;
        int il = 0;
        Teacher teacher = (Teacher) ssoService.tokenUser(token);
        Date date = new Date();
        if (i == 0) {
            //修改请假信息状态
            il = approvalMapper.upStatus(id, teacher.getUser().getName(), date, 1);
            if (il > 0) {
                b = true;
            }
        } else if (i == 1) {
            il = approvalMapper.upStatus(id, teacher.getUser().getName(), date, 2);
            if (il > 0) {
                b = true;
            }
        }
        return b;
    }

    @Override
    public List<Approval> findByStudentId(Long studentId) {
        List<Approval> approvals = approvalMapper.findByStudentId(studentId);
        return approvals;
    }


}
